package com.lichongbing.leetcode.editor.cn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lichongbing.leetcode.editor.cn.domain.TestCase;
import com.lichongbing.leetcode.editor.cn.service.TestCaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/3/6 10:45 下午
 * @description: TODO
 */

@RestController
@RequestMapping("/testcase")
public class TestCaseController {
    @Autowired
    private TestCaseService testCaseService;
    /**
     * 新增
     */
    @PostMapping("/save")
    public JSONObject save(@RequestBody JSONObject json) {
        TestCase testCase = JSONObject.toJavaObject(json, TestCase.class);
        boolean save = testCaseService.save(testCase);
        if (save) {
            return json;
        }else {
            return null;
        }
    }
    @PostMapping("/select")
    public JSONArray select(@RequestBody JSONObject json){
        Long pageNo = Long.valueOf(json.getString("pageNo"));
        Long pageSize = Long.valueOf(json.getString("pageSize"));
        Page<TestCase> page = new Page<>(pageNo, pageSize);
        Page<TestCase> testCasePage = testCaseService.selectPage(page, null);
        List<TestCase> testCases = testCasePage.getRecords();
        String jsonString = JSON.toJSONString(testCases);
        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        return jsonArray;
    }
}
